package com.hskn.hss.module.costdict.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.costdict.entity.CostDict;
import com.hskn.hss.module.costdict.mapper.CostDictMapper;
import com.hskn.hss.module.costdict.service.ICostDictService;
import com.hskn.hss.module.costdictitem.entity.CostDictItem;
import com.hskn.hss.module.costdictitem.mapper.CostDictItemMapper;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-05
 */
@Service
public class CostDictServiceImpl extends ServiceImpl<CostDictMapper, CostDict> implements ICostDictService {

    @Resource
    CostDictMapper costDictMapper;
    @Resource
    CostDictItemMapper costDictItemMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) {

        String uid = LoginUtils.getUser().getUid();
        CostDict costDict = params.getObject("data", CostDict.class);
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        costDict.setId(StringUtils.uuid());
        costDict.setCreatby(uid);
        costDict.setDeptid(deptid);
        int i = costDictMapper.insert(costDict);
        boolean flag = i > 0;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) {
        CostDict costDict = params.getObject("data", CostDict.class);
        int i = costDictMapper.updateById(costDict);
        boolean flag = i > 0;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(String id) {
        try {
            costDictMapper.deleteById(id);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public CostDict getitemByid(String id) throws HssException {
        QueryWrapper<CostDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CostDict::getId, id);
        CostDict costDict = costDictMapper.selectOne(queryWrapper);
        return costDict;
    }

    @Override
    public List<CostDict> getitemListByParentId(String id) throws HssException {
        QueryWrapper<CostDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CostDict::getMenuid, id);
        List<CostDict> costDictList = costDictMapper.selectList(queryWrapper);
        return costDictList;
    }

    @Override
    @DataScope(deptAlias = "empldeptid", userAlias = "creatBy")
    public List<CostDictItem> getitemList(CostDict costDict) throws HssException {
        QueryWrapper<CostDict> queryWrapper = new QueryWrapper<>();
        if (costDict.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(costDict.getParams().get("dataScope").toString())) {
            queryWrapper.lambda().apply(costDict.getParams().get("dataScope").toString());
        }
        queryWrapper.lambda().eq(CostDict::getMenuid, costDict.getId());
        List<CostDict> costDictList = costDictMapper.selectList(queryWrapper);
        Map<String, List<CostDict>> cMap = GroupingByUtils.getGroupingForMapList(costDictList, CostDict::getId);
        QueryWrapper<CostDictItem> costDictItemQueryWrapper = new QueryWrapper<>();
        if (!costDictList.isEmpty()) {
            costDictItemQueryWrapper.lambda().in(CostDictItem::getDictid, cMap.keySet());
        } else {
            return new LinkedList<>();
        }
        List<CostDictItem> costDictItemList = costDictItemMapper.selectList(costDictItemQueryWrapper);
        return costDictItemList;
    }

    @Override
    public Map<String, CostDictItem> getitemForCostDictItemMap(QueryWrapper<CostDict> queryWrapper) {
        List<CostDict> costDictList = costDictMapper.selectList(queryWrapper);
        Map<String, List<CostDict>> cMap = GroupingByUtils.getGroupingForMapList(costDictList, CostDict::getId);
        QueryWrapper<CostDictItem> costDictItemQueryWrapper = new QueryWrapper<>();
        if (!costDictList.isEmpty()) {
            costDictItemQueryWrapper.lambda().in(CostDictItem::getDictid, cMap.keySet());
        } else {
            return new HashMap<>();
        }
        List<CostDictItem> costDictItemList = costDictItemMapper.selectList(costDictItemQueryWrapper);
        Map<String, CostDictItem> stringCostDictItemMap = GroupingByUtils.getGroupingForMapEntity(costDictItemList, CostDictItem::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        return stringCostDictItemMap;
    }
}
